package pers.xx.sbm;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author ：xiexing
 * @description：TODO
 * @date ：2021/2/2 9:19
 */
public class PictureDownload {
    public static void main(String[] args) {
        String url = "http://tkbm.gyzkzx.cn/gyeducation/upload/book/1611739374755.jpg";
        downloadPicture(url);
    }

    //链接url下载图片
    private static void downloadPicture(String urlList) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String imageName = "C:\\Users\\18359\\Desktop\\book\\download20200202\\1611739374754.jpg";

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
